package com.hCode.CnSmart;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class JsonSort {

	 public static void main(String[] args) {
	        String jsonStr = "{\n" +
	                "\t\"systemid\": \"123\",\n" +
	                "\t\"productid\": \"123\",\n" +
	                "\t\"dataArray\": [\n" +
	                "\t\t{\n" +
	                "\t\t\t\"line_num\": 2,\n" +
	                "\t\t\t\"subjectcodecv\": 5,\n" +
	                "\t\t\t\"subjectname\": \"公司\",\n" +
	                "\t\t\t\"subjectid\": \"1001\",\n" +
	                "\t\t\t\"resource_members_id\": \"003\",\n" +
	                "\t\t\t\"resource_members_name\": \"xx\"\n" +
	                "\t\t}, {\n" +
	                "\t\t\t\"line_num\": 2,\n" +
	                "\t\t\t\"subjectcodecv\": 4,\n" +
	                "\t\t\t\"subjectname\": \"产品\",\n" +
	                "\t\t\t\"subjectid\": \"10\",\n" +
	                "\t\t\t\"resource_members_id\": \"002\",\n" +
	                "\t\t\t\"resource_members_name\": \"11xx\"\n" +
	                "\t\t}, {\n" +
	                "\t\t\t\"line_num\": 2,\n" +
	                "\t\t\t\"subjectcodecv\": 3,\n" +
	                "\t\t\t\"subjectname\": \"产品\",\n" +
	                "\t\t\t\"subjectid\": \"10\",\n" +
	                "\t\t\t\"resource_members_id\": \"001\",\n" +
	                "\t\t\t\"resource_members_name\": \"22xx\"\n" +
	                "\t\t}, {\n" +
	                "\t\t\t\"line_num\": 1,\n" +
	                "\t\t\t\"subjectcodecv\": 3,\n" +
	                "\t\t\t\"subjectname\": \"公司\",\n" +
	                "\t\t\t\"subjectid\": \"1001\",\n" +
	                "\t\t\t\"resource_members_id\": \"005\",\n" +
	                "\t\t\t\"resource_members_name\": \"xx\"\n" +
	                "\t\t}, {\n" +
	                "\t\t\t\"line_num\":1,\n" +
	                "\t\t\t\"subjectcodecv\": 1,\n" +
	                "\t\t\t\"subjectname\": \"产品\",\n" +
	                "\t\t\t\"subjectid\": \"10\",\n" +
	                "\t\t\t\"resource_members_id\": \"004\",\n" +
	                "\t\t\t\"resource_members_name\": \"11xx\"\n" +
	                "\t\t}, {\n" +
	                "\t\t\t\"line_num\": 1,\n" +
	                "\t\t\t\"subjectcodecv\": 5,\n" +
	                "\t\t\t\"subjectname\": \"产品\",\n" +
	                "\t\t\t\"subjectid\": \"10\",\n" +
	                "\t\t\t\"resource_members_id\": \"003\",\n" +
	                "\t\t\t\"resource_members_name\": \"22xx\"\n" +
	                "\t\t}\n" +
	                "\t]\n" +
	                "}";
	 
	        JSONObject originalJsonData = JSONObject.parseObject(jsonStr);
	        JSONArray dataArray = originalJsonData.getJSONArray("dataArray");
	        //System.out.println(dataArray);
	        List<JSONObject> jsonObjectList = new ArrayList<>();
	        //将原始数据中的json数组中的json对象存到一个list集合中
	        for (Object o : dataArray) {
	            jsonObjectList.add((JSONObject) o);
	        }
	 
	        //对集合中的json对象进行分组
	        //然后返回一个map集合，key代表组名，value代表该组中的数据
	        Map<String, List<JSONObject>> groupByLineNumData = jsonObjectList.stream()
	                .collect(Collectors.groupingBy(x -> x.getString("line_num")));
	 
	        //遍历所有的分组，对每组内的JSON对象进行排序
	        for (String s : groupByLineNumData.keySet()) {
	            List<JSONObject> list = groupByLineNumData.get(s);
	            JSONArray jsonSorted = jsonArraySort(list.toString());
	            //后续有对排序后的json对象的业务操作
	            //这里只将它输出
	            System.out.println(jsonSorted);
	        }
	 
	    }
	 
	    /**
	     * 对多个JOSN对象进行自定义字段排序
	     *
	     * @param jsonArrStr 要排序的JSON数据
	     * @return 返回排序后的结果
	     */
	    private static JSONArray jsonArraySort(String jsonArrStr) {
	        JSONArray jsonArr = JSON.parseArray(jsonArrStr);
	        JSONArray sortedJsonArray = new JSONArray();
	        List<JSONObject> jsonValues = new ArrayList<JSONObject>();
	        //存放json数组中的每个json对象
	        for (int i = 0; i < jsonArr.size(); i++) {
	            jsonValues.add(jsonArr.getJSONObject(i));
	        }
	 
	        //对集合中的JOSN对象进行自定义排序
	        jsonValues.sort((a, b) -> {
	            String valA = a.getString("resource_members_id");
	            String valB = b.getString("resource_members_id");
	            return valA.compareTo(valB);
	        });
	 
	        for (int i = 0; i < jsonArr.size(); i++) {
	            sortedJsonArray.add(jsonValues.get(i));
	        }
	        return sortedJsonArray;
	    }

}
